package com.example.neusoft.zyfypt_wk.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.neusoft.zyfypt_wk.R;
import com.example.neusoft.zyfypt_wk.bean.LoginBean;
import com.example.neusoft.zyfypt_wk.iface.LoginListener;
import com.example.neusoft.zyfypt_wk.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText etuser,etpass;
    private Switch sw;
    private SharedPreferences sp;
    private String username="",password="",sessionID="";
    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID=loginBean.getSessionid();
            System.out.println("----sessionID="+sessionID);
            if(sessionID!=null) {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                saveSP();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            else
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

        }
    };

    private void saveSP() {

        Boolean remember=sw.isChecked();
        SharedPreferences.Editor  editor=sp.edit();
        editor.putString("name",username);
        editor.putString("pass",password);
        editor.putBoolean("remember",remember);
        editor.putString("sessionID",sessionID);
        editor.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sp=getSharedPreferences("login",MODE_PRIVATE);
        init();
        readSP();
    }

    private void readSP() {
        String strname=sp.getString("name",null);
        String strpass=sp.getString("pass",null);
        Boolean remember=sp.getBoolean("remember",false);
        sw.setChecked(remember);
        if(remember)
        {
            etuser.setText(strname);
            etpass.setText(strpass);
        }
    }

    private void init()
    {
        findViewById(R.id.btnlogin).setOnClickListener(this);
        findViewById(R.id.btnregister).setOnClickListener(this);
        etuser=(EditText)findViewById(R.id.editText);
        etpass=(EditText)findViewById(R.id.editText2);
        sw=(Switch)findViewById(R.id.switch1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btnlogin://登录
                username=etuser.getText().toString();
                password=etpass.getText().toString();
                LoginModel loginModel=new LoginModel();
                loginModel.getLoginResult(username,password,loginListener);
                
                break;
            case R.id.btnregister://注册
                Toast.makeText(this, "注册", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);

                break;
        }
    }
}
